package com.hubu.core;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class DatabaseManager {





    private static volatile DatabaseManager databaseManager;
    private final int dbCount;
    private Database currentDatabase;
    public Map<Integer, Database> dbMap = new HashMap<>();
    public DatabaseManager(int dbCount) {
        this.dbCount = dbCount;
        for (int i = 0; i < dbCount; i++) {
            Database database = new Database(i + "");
            dbMap.put(i, database);
        }
    }
    /**
     * 选择使用的database
     */
    public Database activeDatabase(int dbId) {
        currentDatabase = dbMap.get(dbId);
        return currentDatabase;
    }
    public int size(){
        int total=0;
        Set<Integer> keySet = dbMap.keySet();
        Iterator<Integer> iterator = keySet.iterator();
        while(iterator.hasNext()){
            Integer next = iterator.next();
            Database database = dbMap.get(next);
            total+= database.size();
        }
        return total;
    }
    public static DatabaseManager getInstance(int dbCount) {
        if(databaseManager==null){
            synchronized (DatabaseManager.class){
                if(databaseManager==null){
                    databaseManager=new DatabaseManager(dbCount);
                }
            }
        }
        return databaseManager;
    }


}
